﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace WITD
{
    /// <summary>
    /// Interaction logic for ChooseNewDefaultWIT.xaml
    /// </summary>
    public partial class ChooseNewDefaultWIT : Window
    {
        Project CurrentProject;
        String SelectedCategoryName;
        String WITToExclude;
        String _selectedWITToReplaceDefault;
        WorkItemCategory CatManager;
        Window toolWindow;
        Opaque hider;

        /// <summary>
        /// The name of the selected WorkItemType with which to replace the default.
        /// </summary>
        public String SelectedWITToReplaceDefault
        {
            get
            {
                return _selectedWITToReplaceDefault;
            }
        }

        /// <summary>
        /// Window to allow user to select a new default work item type from the list of work item types already in the category.
        /// </summary>
        /// <param name="project">The current team project.</param>
        /// <param name="categoryName">The selected category name.</param>
        /// <param name="witName">The work item type name to be replaced.</param>
        /// <param name="parent">The owning Window object instance. It must have been shown at least once to avoid an Exception.</param>
        public ChooseNewDefaultWIT(Project project, String categoryName, String witName, Window parent)
        {
            InitializeComponent();
            CurrentProject = project;
            SelectedCategoryName = categoryName;
            WITToExclude = witName;
            CatManager = new WorkItemCategory(CurrentProject);
            Owner = parent;
            PopulateList(WITToExclude);
            hider = new Opaque(this);
        }

        /// <summary>
        /// Populates a list box with all available WorkItemTypes, except the specified WorkItemType.
        /// </summary>
        /// <param name="witNameToExclude">The name of the WorkItemType to exclude from the list.</param>
        private void PopulateList(String witNameToExclude)
        {
            System.Collections.ArrayList wits = CatManager.GetAvailableWITsForCategory(SelectedCategoryName, witNameToExclude);
            foreach (String wit in wits)
            {
                lstWITDs.Items.Add(wit);
            }
        }

        /// <summary>
        /// Cancels this operation and closes the window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }

        /// <summary>
        /// Returns true to the caller and closes the window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOkay_Click(object sender, RoutedEventArgs e)
        {
            //get selected wit
            DialogResult = true;
            Close();
        }

        /// <summary>
        /// Stores the currently selected work item type everytime the selection changes.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lstWITDs_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lstWITDs.SelectedItem != null)
            {
                _selectedWITToReplaceDefault = lstWITDs.SelectedItem as string;
            }
        }

        /// <summary>
        /// Places an opaque window over the current window to focus the user's attention on the new form about to open and claim focus.
        /// </summary>
        private void ObscureMainWindow(Window newWindow)
        {
            Topmost = false;
            toolWindow = newWindow;
            if (newWindow != null) newWindow.Topmost = true;
            hider.Height = this.Height;
            hider.Width = this.Width;
            hider.Top = this.Top;
            hider.Left = this.Left;
            hider.Cursor = Cursors.Wait;
            hider.Show();
        }

        /// <summary>
        /// Hides the opaque window without destroying it, making the current form the focus once again.
        /// </summary>
        private void UnobscureMainWindow()
        {
            hider.Hide();
            toolWindow = this;
            this.Topmost = true;
            this.Focus();
        }

        
    }
}
